#include "stm32f10x.h"
/**
  * @brief PWM初始化
  * @param
  * @param 
  * @retval 
  */
void PWM_Init(uint16_t period,uint16_t prescaler)
{
	//1.使能定时器以及端口时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO,ENABLE);//端口复用值要开启
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
	
	//引脚重映射(PIN Remap)-部分备用功能 映射
	GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);//TIM3 Partial Alternate Function mapping
	
	//2.初始化GPIOB,到具体引脚
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_5;//GPIOB_Pin5
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
	
	//3.定时器参数配置（自动重装载值ARR,分频系数PRC,计数方式等）
	//定时器的基础Base初始化
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;//1分频
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period=period;//arr
	TIM_TimeBaseInitStruct.TIM_Prescaler=prescaler;//psc
	TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStruct);
	
	
	//4.配置定时器OC模式
	TIM_OCInitTypeDef TIM_InitStruct;
	TIM_OCStructInit(&TIM_InitStruct);//默认值配置
	TIM_InitStruct.TIM_OutputState=ENABLE;
	TIM_InitStruct.TIM_OCMode=TIM_OCMode_PWM1;
	TIM_InitStruct.TIM_OCPolarity=TIM_OCPolarity_High;//极性不翻转 当cnt==arr时
	TIM_OC2Init(TIM3,&TIM_InitStruct);
	
	//5.使能TIMx的在CCRx上的预装载寄存器
	TIM_OC2PreloadConfig(TIM3,ENABLE);
	TIM_ARRPreloadConfig(TIM3,ENABLE);
	
	//开启定时器
	TIM_Cmd(TIM3,ENABLE);
}
void PWM_SetCompare2(uint16_t compare){
	TIM_SetCompare2(TIM3, compare);
}
